package bairui.international.console.web.controller.branch.branch.area;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.querydsl.QSort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;

import bairui.international.model.domain.jpa.QTB_Town;
import bairui.international.model.domain.jpa.TB_City;
import bairui.international.model.domain.jpa.TB_Province;
import bairui.international.model.domain.jpa.TB_Town;
import bairui.international.service.Service_Town;

/**
 *
 * 添加
 *
 * @generated
 * 
 * @see STest_C_BranchCompanyArea_Add_Page 测试
 *
 */
@Controller
public class C_BranchCompanyArea_Add_Page
 extends SC_BranchCompanyArea_Add_Page 
{
	
	@Autowired private Service_Town service_Town;
	
	/**
	 * 分页列表
	 */
	@SuppressWarnings("serial")
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request, 
		@PathVariable(name=Page.PathVariableName.BranchId) long branchId
	) throws Exception {
		org.springframework.data.domain.Page<TB_Town> page = service_Town.page(
				it->filter(it, request), 
				Page.Output.page(request), 
				it->sort(it, request)
			);
		//
		return Page.Output.output(
				page.map(it->new Page.Output.Model(it){
					@Override protected void assign_before() {
					}
					@Override protected TB_Province assignProvince() {
						return it.getCity().getProvince();
					}
					@Override protected TB_City assignCity() {
						return it.getCity();
					}
				})
			);
	}
	
	/*
	 * 过滤
	 */
	static BooleanBuilder filter(QTB_Town it, HttpServletRequest request){
		return Page.Filter.query(it, request, new Page.Filter.FilterExtends(){
					@Override public Predicate gainProvince(TB_Province province) {
						return it.city.province.eq(province);
					}
				})
				.and(it.branchCompany.isNull());
	}
	
	/*
	 * 排序
	 */
	static Sort sort(QTB_Town it, HttpServletRequest request){
		return Page.Output.sort(it, request, new Page.Output.OrderExtends(){
					@Override public Sort gainProvince(Direction direction) {
						return sort(direction, it.city.province.name);
					}
					@Override public Sort gainCity(Direction direction) {
						return sort(direction, it.city.name);
					}
				})
				.and(new QSort(it.city.province.id.asc()))
				.and(new QSort(it.city.id.asc()))
				.and(new QSort(it.id.asc()));
	}
	
	

}
